<template>
    <el-dialog :title="isEdit?'编辑专运线管理':'新增专运线管理'" :visible.sync="dialogForm" width="720px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="专运线" prop="resourceNo">
                        <el-input v-model="form.resourceNo" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属单位" prop="orgId">
                        <auto-search-org v-model="form.orgId"
                                         @select="v=>{form.orgId=v;form.deptId='';form.deptName='';}"
                                         @selectName="v=>form.orgName=v"
                                         :disabled="true"
                                         :name="form.orgName"></auto-search-org>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="所属部门" prop="deptId">
                        <search-dept v-model="form.deptId"
                                     @select="v=>{form.deptId=v;}"
                                     @selectName="v=>form.deptName=v"
                                     :orgId="form.orgId"
                                     :disabled="!form.orgId"
                                     :name="form.deptName"></search-dept>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="山侧方向" prop="riverSideRoadDire">
                        <el-select v-model="form.riverSideRoadDire" placeholder="请选择" clearable>
                            <el-option v-for="[key, val] in _dictSOrB" :key="key" :label="val" :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="最大车位数" prop="maxParkingNum">
                        <!--<el-input v-model="form.typeName" clearable></el-input>-->
                        <el-input-number :controls="false" v-model="form.maxParkingNum"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">   
                    <el-form-item label="河侧方向" prop="sidehilRoadDire">
                        <el-select v-model="form.sidehilRoadDire" placeholder="请选择" clearable>
                            <el-option v-for="[key, val] in _dictSOrB" :key="key" :label="val" :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="是否可用" prop="valid">
                        <el-radio v-model="form.valid" :label="true">是</el-radio>
                        <el-radio v-model="form.valid" :label="false">否</el-radio>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="resourceDescribe">
                        <el-input type="textarea" :rows="3" style="width: 560px" placeholder="请输入内容"
                                  :maxLength="$maxLength"
                                  v-model="form.resourceDescribe"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addSpecialTransportLine, updateSpecialTransportLine } from "@/api/baseResource/baseResource";
    import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
    import autoSearchDic from '@/components/autocomplete/autoSearchDic'
    import autoSearchTer from '@/components/autocomplete/autoSearchTer'
    import searchDept from '@/components/autocomplete/autoSearchDept'
    import { maxLength, sortLength } from "@/utils/rules";
    import {PRIVATE_LINE} from '@/utils/Reg'

    export default {
        name: "roleForm",
        components: { autoSearchOrg, searchDept, autoSearchDic, autoSearchTer },
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    resourceNo: [{ required: true,validator:PRIVATE_LINE, trigger: 'blur' }, 
                                {max:2,message:'最多输入2个字符',trigger:'blur'}],
                    maxParkingNum: [{ required: true, message: '请输入最大车位数', trigger: 'blur' }],
                    sidehilRoadDire: [{ required: true, message: '请选择河侧方向', trigger: 'blur' }, sortLength],
                    riverSideRoadDire: [{ required: true, message: '请选择山侧方向', trigger: 'blur' }, sortLength],
                    valid: [{ required: true, message: '请选择是否有用', trigger: 'blur' }],
                    orgId: [{ required: true, message: '请选择单位', trigger: 'change' }],
                    deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
                    resourceDescribe: [maxLength]
                },
                dialogForm: false,
                isEdit: false,
            }
        },
        methods: {
            clearForm() {
                return {
                    resourceNo: "",
                    maxParkingNum: "",
                    sidehilRoadDire: "",
                    riverSideRoadDire: "",
                    orgId: this.$store.state.user.currentUser.org && this.$store.state.user.currentUser.org.id,
                    orgName: this.$store.state.user.currentUser.org && this.$store.state.user.currentUser.org.name,
                    deptId: "",
                    deptName: "",
                    valid: true,
                    resourceDescribe: "",
                }
            },
            // 保存
            handleSubmit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.isEdit ? this.handleToEdit() : this.handleToAdd()
                    } else {
                        this.$message({
                            message: '请检查表单',
                            type: 'error'
                        })
                    }
                })
            },
            // 新增
            handleToAdd() {
                addSpecialTransportLine(this.form).then(res => {
                    this.$message({ message: '新增成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 编辑
            handleToEdit() {
                updateSpecialTransportLine(this.form.id, this.form).then(res => {
                    this.$message({ message: '编辑成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$refs.form.resetFields()
                    this.isEdit = false
                    this.form = this.clearForm()
                    this.$emit('clear')
                }
            },
        },
    }
</script>

<style scoped>

</style>
